<html>
<head><meta charset="utf-8"><title>repr(C,packed) structs have incorrect size · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html">repr(C,packed) structs have incorrect size</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228588903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228588903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228588903">(Mar 03 2021 at 13:17)</a>:</h4>
<p><code>size_of::&lt;Foo&gt;()</code> for the struct below returns 12:</p>
<div class="codehilite"><pre><span></span><code>#[repr(C,packed(4))]
struct Foo {
    a: u16,
    b: u32,
    c: u16,
}
</code></pre></div>
<p>But gcc's sizeof for the c version of the struct returns 8 (<a href="https://godbolt.org/z/zrjqYe">https://godbolt.org/z/zrjqYe</a>):</p>
<div class="codehilite"><pre><span></span><code>struct foo {
    uint16_t a;
    uint32_t b;
    uint16_t c;
} __attribute__((packed));

int32_t blub(struct foo *bar) {
    return sizeof(struct foo);
}
</code></pre></div>
<p>Did I misunderstand the semantics of Rust's C and packed reprs, or is this a rustc bug? After investigating writes of the struct it seems that rustc pads the first u16 to 32bit, I guess because it tries to align the u32 despite the packed annotation.</p>



<a name="228589321"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228589321" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228589321">(Mar 03 2021 at 13:20)</a>:</h4>
<p>or is the 4 supposed to say "every member is aligned to a 4 byte boundary"?</p>



<a name="228590208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590208">(Mar 03 2021 at 13:27)</a>:</h4>
<p><code>packed</code> can only lower the alignment, if its larger than the fields' alignment, then its a no-op. (as per <a href="https://doc.rust-lang.org/stable/reference/type-layout.html#the-alignment-modifiers">https://doc.rust-lang.org/stable/reference/type-layout.html#the-alignment-modifiers</a>)</p>



<a name="228590306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590306">(Mar 03 2021 at 13:28)</a>:</h4>
<p>so your original type's alignment is 4, you asked to lower it to 4, so its laid out as if there was no packed attribute in the first place.</p>



<a name="228590379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590379">(Mar 03 2021 at 13:29)</a>:</h4>
<p>so packed(4) says "lower the alignment of all members to 4"?</p>



<a name="228590440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590440">(Mar 03 2021 at 13:29)</a>:</h4>
<p>Lower the alignment of the type and its fields to 4, yes.</p>



<a name="228590516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590516">(Mar 03 2021 at 13:30)</a>:</h4>
<p>Shouldn't this print 12 then?</p>
<div class="codehilite"><pre><span></span><code>#[repr(packed(4))]
struct Foo {
    a: u16,
    b: u32,
    c: u16,
}

fn main() {
    println!(&quot;sizeof::&lt;Foo&gt;={}&quot;, size_of::&lt;Foo&gt;());
}
</code></pre></div>



<a name="228590663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590663">(Mar 03 2021 at 13:30)</a>:</h4>
<p>So that's using <code>repr(Rust)</code> which does not guarantee any field ordering in particular – you're seeing a field order optimization going on.</p>



<a name="228590756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590756">(Mar 03 2021 at 13:31)</a>:</h4>
<p>ah so a and c are put next to each other, so reads to them will be properly aligned</p>



<a name="228590764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590764">(Mar 03 2021 at 13:31)</a>:</h4>
<p>So in practice you have a <code>(u32, u16, u16)</code> not <code>(u16, padding, u32, u16, padding)</code></p>



<a name="228590787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590787">(Mar 03 2021 at 13:31)</a>:</h4>
<p>yeah.</p>



<a name="228590920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590920">(Mar 03 2021 at 13:32)</a>:</h4>
<p>Is the "lower the alignment of the type and its fields to 4" behaviour documented anywhere?</p>



<a name="228590922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590922">(Mar 03 2021 at 13:32)</a>:</h4>
<p>If you specify <code>repr(C, packed(2))</code> that will reduce the alignment of <code>b</code> field to <code>2</code> and the structure then will fit into 8 bytes as well.</p>



<a name="228590982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228590982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228590982">(Mar 03 2021 at 13:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="394290">Benedikt Radtke</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size/near/228590920">said</a>:</p>
<blockquote>
<p>Is the "lower the alignment of the type and its fields to 4" behaviour documented anywhere?</p>
</blockquote>
<p>I linked to the relevant section of the reference in an earlier message. Its documented there.</p>



<a name="228591061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228591061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228591061">(Mar 03 2021 at 13:33)</a>:</h4>
<p>Specifically this paragraph: </p>
<blockquote>
<p>For packed, if the specified alignment is greater than the type's alignment without the packed modifier, then the alignment and layout is unaffected. The alignments of each field, for the purpose of positioning fields, is the smaller of the specified alignment and the alignment of the field's type.</p>
</blockquote>



<a name="228591350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228591350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228591350">(Mar 03 2021 at 13:35)</a>:</h4>
<p>Ah I have read that paragraph wrong, thanks for the heads-up!</p>



<a name="228592159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228592159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228592159">(Mar 03 2021 at 13:40)</a>:</h4>
<p>Unfortunately, using <code>repr(C, packed(2))</code> causes rustc to emit 2byte-reads for all members in the struct, even if the member is wider and properly aligned. Is there a way around that?</p>



<a name="228592624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228592624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228592624">(Mar 03 2021 at 13:43)</a>:</h4>
<p>Why do you say its properly aligned? Field <code>b</code> can't possibly be aligned to its natural alignment if its alignment has been lowered.</p>



<a name="228592938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228592938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228592938">(Mar 03 2021 at 13:45)</a>:</h4>
<p>Well, it can at runtime happen to be aligned to 4, 8 or whatever larger alignment, but it can also happen to fall on an address that happens to be aligned to only 2. the only way to check it would be to add branches, wouldn't it?</p>



<a name="228593384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228593384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228593384">(Mar 03 2021 at 13:48)</a>:</h4>
<p>My sample struct's u32 is not properly aligned, but there are structs that are, let's consider</p>
<div class="codehilite"><pre><span></span><code>struct Bar {
    a: u16,
    b: u16,
    c: u32,
}
</code></pre></div>
<p>gcc's packed attribute would make it assume that Bar is always 4-byte aligned, and use a 4-byte read for the member c, unless I explicitly specify <code>-mno-unaligned-access</code></p>



<a name="228593787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228593787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228593787">(Mar 03 2021 at 13:50)</a>:</h4>
<p>So it chooses to generate an unaligned load rather than assuming (or computing) that <code>c</code> is aligned to 4.</p>



<a name="228593900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228593900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228593900">(Mar 03 2021 at 13:51)</a>:</h4>
<p>Whether the codegen will emit an unaligned load (on architectures where this is permitted) or not depends entirely on its implementation and cost models.</p>



<a name="228593981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228593981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228593981">(Mar 03 2021 at 13:51)</a>:</h4>
<p>it generates a load that assumes the alignment is proper, yeah</p>



<a name="228594259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228594259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228594259">(Mar 03 2021 at 13:53)</a>:</h4>
<p>I would suggest testing this on architectures where unaligned loads are not transparently handled.</p>



<a name="228594477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228594477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228594477">(Mar 03 2021 at 13:54)</a>:</h4>
<p>I don't think there's going to be an obvious way to get the backend to consistently generate unaligned loads… you could maybe achieve it by specifying a CPU model where unaligned loads are cheaper than usual, compared to regular aligned loads.</p>



<a name="228594478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228594478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228594478">(Mar 03 2021 at 13:54)</a>:</h4>
<p>I do, I am using armv7 and 8 - gcc's code triggers an alignment exception if I force it to use an unaligned pointer</p>



<a name="228594545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228594545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228594545">(Mar 03 2021 at 13:55)</a>:</h4>
<p>Isn't gcc buggy then?</p>



<a name="228594793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228594793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228594793">(Mar 03 2021 at 13:56)</a>:</h4>
<p>I'd say "it depends", their packed docs say "it removes the padding between the members", but rules as written the alignment requirements on the container could remain, depending on how you read it</p>



<a name="228594835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228594835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228594835">(Mar 03 2021 at 13:56)</a>:</h4>
<p>I guess it could maybe draw inspiration from surrounding environment to deduce that the field must be aligned somehow, but in a general case where the type is put into, say, an array of multiple elements, the compiler can't assume alignment of the type is larger than it really is.</p>



<a name="228594927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228594927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228594927">(Mar 03 2021 at 13:57)</a>:</h4>
<p>gcc's way of doing things allows you to use pointers to structs which are backed by MMIO devices, which do not approve fragmented reads or writes</p>



<a name="228594976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228594976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228594976">(Mar 03 2021 at 13:57)</a>:</h4>
<p>If you're testing by allocating a value on the stack, it could be applying optimisations such as SROA to unpack the structure into its constituent components in the first place.</p>



<a name="228595130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228595130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228595130">(Mar 03 2021 at 13:58)</a>:</h4>
<p>Ah, I see now what the context is. yeah <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>



<a name="228595450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228595450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228595450">(Mar 03 2021 at 14:00)</a>:</h4>
<p><span class="user-mention silent" data-user-id="394290">Benedikt Radtke</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size/near/228594793">said</a>:</p>
<blockquote>
<p>I'd say "it depends", their packed docs say "it removes the padding between the members", but rules as written the alignment requirements on the container could remain, depending on how you read it</p>
</blockquote>
<p>You could verify the alignment of the container type with <code>_Alignof(...)</code></p>



<a name="228595646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228595646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228595646">(Mar 03 2021 at 14:01)</a>:</h4>
<p>Ah that is nice!</p>



<a name="228595783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228595783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228595783">(Mar 03 2021 at 14:02)</a>:</h4>
<p>By the way, does rust truly not guarantee any alignment requirements on repr(c, packed(1)) structs on the stack?</p>



<a name="228595894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228595894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228595894">(Mar 03 2021 at 14:02)</a>:</h4>
<p>rules as written I must not pass pointers to these to c libraries, because they might have alignment requirements</p>



<a name="228596075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228596075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228596075">(Mar 03 2021 at 14:03)</a>:</h4>
<p>The struct type's alignment gets lowered to the requested alignment. <code>packed(1)</code> causes the alignment to become 1, and thus yes, the structure can end up at any address boundary.</p>



<a name="228596225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228596225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228596225">(Mar 03 2021 at 14:04)</a>:</h4>
<p>that effectively means "you cannot reliably pass pointers to packed structs to c libraries", doesn't it?</p>



<a name="228596241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228596241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228596241">(Mar 03 2021 at 14:04)</a>:</h4>
<p>I would expect <code>repr(C, packed)</code> to be equivalent in how it affects the alignment of the container and its fields to what C does for similar attributes.</p>



<a name="228596318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228596318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228596318">(Mar 03 2021 at 14:05)</a>:</h4>
<p>do you think we could get that guaranteed in the documentation? <span aria-label="innocent" class="emoji emoji-1f607" role="img" title="innocent">:innocent:</span></p>



<a name="228596524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228596524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228596524">(Mar 03 2021 at 14:06)</a>:</h4>
<p><a href="https://godbolt.org/z/1K5hG5">https://godbolt.org/z/1K5hG5</a> fwiw gcc does lower the alignment of the struct itself too.</p>



<a name="228596586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228596586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228596586">(Mar 03 2021 at 14:06)</a>:</h4>
<p>godbolt builds with -mno-unaligned-access</p>



<a name="228596753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228596753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228596753">(Mar 03 2021 at 14:07)</a>:</h4>
<p>without it my arm-none-eabi-gcc 10.2.0 happily does 32bit reads on packed uint32_ts</p>



<a name="228596842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228596842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228596842">(Mar 03 2021 at 14:08)</a>:</h4>
<p>specifying <code>-munaligned-access</code> still computes the alignment as 1.</p>



<a name="228597436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228597436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228597436">(Mar 03 2021 at 14:11)</a>:</h4>
<p><span class="user-mention silent" data-user-id="394290">Benedikt Radtke</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size/near/228596318">said</a>:</p>
<blockquote>
<p>do you think we could get that guaranteed in the documentation? <span aria-label="innocent" class="emoji emoji-1f607" role="img" title="innocent">:innocent:</span></p>
</blockquote>
<p>This is the position of the language team cf. <a href="https://github.com/rust-lang/rust/issues/81996#issuecomment-780047179">https://github.com/rust-lang/rust/issues/81996#issuecomment-780047179</a> so I would say just file an issue if its not yet documented somewhere (but also see the rest of the discussion on that thread,as its very relevant to your question)</p>



<a name="228597713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228597713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228597713">(Mar 03 2021 at 14:13)</a>:</h4>
<p>true that, but 10.2 still does the 32bit-read: <a href="https://godbolt.org/z/93a43T">https://godbolt.org/z/93a43T</a></p>



<a name="228598174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228598174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228598174">(Mar 03 2021 at 14:15)</a>:</h4>
<p>amusingly, 10.2.1 does byte-wise reads</p>



<a name="228598276"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228598276" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228598276">(Mar 03 2021 at 14:16)</a>:</h4>
<p>9.3 does a 32bit read</p>



<a name="228598645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228598645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228598645">(Mar 03 2021 at 14:18)</a>:</h4>
<p>ah no wait, the <code>none</code> and <code>linux</code> targets differ, maybe the kernel is doing some emulation magic for the unaligned reads?</p>



<a name="228598774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228598774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228598774">(Mar 03 2021 at 14:19)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size/near/228597436">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="394290">Benedikt Radtke</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size/near/228596318">said</a>:</p>
<blockquote>
<p>do you think we could get that guaranteed in the documentation? <span aria-label="innocent" class="emoji emoji-1f607" role="img" title="innocent">:innocent:</span></p>
</blockquote>
<p>This is the position of the language team cf. <a href="https://github.com/rust-lang/rust/issues/81996#issuecomment-780047179">https://github.com/rust-lang/rust/issues/81996#issuecomment-780047179</a> so I would say just file an issue if its not yet documented somewhere (but also see the rest of the discussion on that thread,as its very relevant to your question)</p>
</blockquote>
<p>Thanks, I'll give it a read!</p>



<a name="228615571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228615571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228615571">(Mar 03 2021 at 15:54)</a>:</h4>
<p>note that, as far as I'm aware, rust does not offer an intrinsic for an unaligned volatile access, so you cannot correctly do mmio with unaligned values</p>



<a name="228615931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228615931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228615931">(Mar 03 2021 at 15:56)</a>:</h4>
<p>I know, but all my structs <em>are</em>  4-byte aligned, it is just that I must specify an alignment of 2 because they have pairs of u16 as members</p>



<a name="228616038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228616038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228616038">(Mar 03 2021 at 15:56)</a>:</h4>
<p>(except the copies on the stack, only god and you know how rustc will align them on my stack)</p>



<a name="228618026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228618026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228618026">(Mar 03 2021 at 16:07)</a>:</h4>
<p>then you probably should model individual addresses and not try to make it a whole single struct</p>



<a name="228623406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228623406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Benedikt Radtke <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228623406">(Mar 03 2021 at 16:36)</a>:</h4>
<p>that works for MMIO registers, but not if you have to pass a pointer to a c lib that does wide reads and writes on your pointer (?)</p>



<a name="228630536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228630536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228630536">(Mar 03 2021 at 17:19)</a>:</h4>
<p>I don't think its at all different from a c lib doing wide reads of its own pointers to those same types… Unless it takes care to allocate the structure in a way that would permit it, which I'm pretty confident no implementation can guarantee.</p>



<a name="228630623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228630623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228630623">(Mar 03 2021 at 17:19)</a>:</h4>
<p>primarily because alignment is not only a concern for the compiler, its a concern the entire toolchain has to deal with.</p>



<a name="228642370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/repr%28C%2Cpacked%29%20structs%20have%20incorrect%20size/near/228642370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/repr(C.2Cpacked).20structs.20have.20incorrect.20size.html#228642370">(Mar 03 2021 at 18:29)</a>:</h4>
<p>without knowing more about the C code I'm not sure I can offer any advice.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>